package cn.wjzhang.middle.modules.global.service.weather;


import cn.wjzhang.core.beans.vo.Result;
import cn.wjzhang.core.constant.CommonConstant;
import cn.wjzhang.core.enums.ErrorCodeEnum;
import cn.wjzhang.core.exception.BootException;
import cn.wjzhang.middle.modules.global.beans.request.SeniverseRequest;
import cn.wjzhang.middle.modules.global.beans.request.weather.WeatherRequest;
import cn.wjzhang.middle.modules.global.beans.response.SeniverseResponse;
import cn.wjzhang.middle.modules.global.beans.response.weather.WeatherResponse;
import cn.wjzhang.middle.modules.global.service.GlobalService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * ========================
 * 心知天气
 *
 * @author wjzhang
 * @version v1.0
 * ========================
 * @date 2023/7/3 10:06
 */
@Service("Seniverse")
@Slf4j
public class SeniverseServiceImpl extends GlobalService<WeatherRequest> {

    @Override
    protected void serviceValidate(JSONObject requestJson) throws IllegalArgumentException {

    }

    @Override
    public Result<?> dataProcess(JSONObject requestJson) {
        // 转为实体类
        WeatherRequest request = JSON.toJavaObject(requestJson, WeatherRequest.class);

        // 1、创建签名验签授权任务
        WeatherResponse response = null;
        try {
            response = request.execute();
        } catch (BootException e) {
            e.printStackTrace();
            return Result.error(ErrorCodeEnum.UNKNOWN_ERROR.getMessage());
        }
        if (!response.isSuccess()) {
            return Result.error(response.getMessage());
        }
        return Result.ok(response.getResults());
    }
}
